package gtug.duesseldorf.client.ui;

import gtug.duesseldorf.client.Wave_webclient;
import gtug.duesseldorf.client.model.ClientWaveId;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HeaderPanel extends Composite {

	private final Wave_webclient m_client;

	private final HeaderLabel m_lbWaveName;

	private final FlowPanel m_participantsPanel;

	private final Button m_btAddParticipant;

	private final Map<String, Hyperlink> m_name2link = new HashMap<String, Hyperlink>();

	private ClientWaveId m_waveId;

	public HeaderPanel(Wave_webclient client) {
		m_client = client;

		VerticalPanel vp = new VerticalPanel();
		vp.setBorderWidth(1);
		vp.setWidth("100%");

		m_lbWaveName = new HeaderLabel();

		vp.add(m_lbWaveName);

		m_participantsPanel = new FlowPanel();
		m_participantsPanel.setStylePrimaryName("participant-panel");
		// DOM.setStyleAttribute(m_participantsPanel.getElement(),
		// "backgroundColor", "yellow");
		// m_participantsPanel.setSpacing(5);
		vp.add(m_participantsPanel);

		m_btAddParticipant = new Button("Add");
		m_participantsPanel.add(m_btAddParticipant);
		m_btAddParticipant.addClickHandler(new ClickHandler() {

			private AddParticipantDialog m_dialog;

			@Override
			public void onClick(ClickEvent event) {
				if (m_dialog == null) {
					m_dialog = new AddParticipantDialog(m_client);
				}

				m_dialog.showOverParent(m_btAddParticipant);
			}
		});

		initWidget(vp);
	}

	public void addParticipant(final String participant) {
		if (m_name2link.keySet().contains(participant) == false) {
			final Hyperlink lkProfile = new Hyperlink(participant, false, participant);
			m_name2link.put(participant, lkProfile);
			m_participantsPanel.add(lkProfile);
			lkProfile.addClickHandler(new ClickHandler() {

				private ProfileDialog m_profileDialog;

				@Override
				public void onClick(ClickEvent event) {
					if (m_profileDialog == null) {
						m_profileDialog = new ProfileDialog();
					}

					m_profileDialog.setProfileName(participant);
					m_profileDialog.showOverParent(lkProfile);
				}
			});
		}
	}

	public void removeParticipant(String participant) {
		if (m_name2link.keySet().contains(participant)) {
			Hyperlink lkProfile = m_name2link.get(participant);
			m_participantsPanel.remove(lkProfile);
			m_name2link.remove(participant);
		}
	}

	public void clear() {
		m_name2link.clear();

		m_lbWaveName.setHeaderText(null);
		m_participantsPanel.clear();
		m_participantsPanel.add(m_btAddParticipant);
	}

	public void setWave(ClientWaveId waveId) {
		if (m_waveId == null || (m_waveId != null && m_waveId.equals(waveId) == false)) {

			m_lbWaveName.setHeaderText(waveId == null ? "No Wave Selected" : waveId.getId());

		}

		m_waveId = waveId;

		clear();
	}
}
